package com.deyou.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.deyou.Constants;
import com.deyou.R;
import com.deyou.base.BaseActivity;
import com.deyou.callback.ServerApi;
import com.deyou.model.CityBean;
import com.deyou.model.LzyResponse;
import com.deyou.model.SimpleResponse;
import com.deyou.util.AESUtil;
import com.deyou.util.AndroidUtils;
import com.deyou.util.Logger;
import com.deyou.util.SPUtils;
import com.deyou.util.Urls;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class AddPaymentAccountActivity extends BaseActivity {

    @BindView(R.id.iv_left_back)
    ImageView ivLeftBack;
    @BindView(R.id.tv_mid_title)
    TextView tvMidTitle;
    @BindView(R.id.edt_bank_name)
    EditText edtBankName;
    @BindView(R.id.edt_bank_addr)
    EditText edtBankAddr;
    @BindView(R.id.edt_swift_code)
    EditText edtSwiftCode;
    @BindView(R.id.bank_code_need_layout)
    LinearLayout bankCodeNeedLayout;
    @BindView(R.id.tv_account_title)
    TextView tvAccountTitle;
    @BindView(R.id.edt_card_code)
    EditText edtCardCode;
    @BindView(R.id.tv_account_name)
    TextView tvAccountName;
    @BindView(R.id.edt_code_name)
    EditText edtCodeName;
    @BindView(R.id.tips_layout)
    LinearLayout tipsLayout;
    @BindView(R.id.btn_commit)
    TextView btnCommit;

    private int paymentType = 0; // 1 支付宝  2 银行卡 3 paypal

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_payment_account);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        paymentType = getIntent().getExtras().getInt("payment_type");
        bankCodeNeedLayout.setVisibility(View.GONE);
        tipsLayout.setVisibility(View.GONE);
        switch (paymentType) {
            case 1:
                tvMidTitle.setText("支付宝");
                tvAccountTitle.setText("支付宝账号");
                edtCardCode.setHint("请输入您的支付宝账号");
                tvAccountName.setText("真实姓名");
                edtCodeName.setHint("请输入支付宝认证的实名");
                break;
            case 2:
                tvMidTitle.setText("银行卡");
                bankCodeNeedLayout.setVisibility(View.VISIBLE);
                tipsLayout.setVisibility(View.VISIBLE);
                break;
            case 3:
                tvMidTitle.setText("PayPal");
                tvAccountTitle.setText("PayPal账号");
                edtCardCode.setHint("请输入您的支付宝账号");
                tvAccountName.setText("真实姓名");
                edtCodeName.setHint("请输入PayPal认证的实名");
                break;
        }
    }

    @OnClick({R.id.iv_left_back, R.id.btn_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_left_back:
                finish();
                break;
            case R.id.btn_commit:
                if (TextUtils.isEmpty(edtCardCode.getText().toString()) || TextUtils.isEmpty(edtCodeName.getText().toString())) {
                    showToast("请先输入正确的" + (TextUtils.isEmpty(edtCardCode.getText().toString()) ? "支付宝账号" : "支付宝认证的实名"));
                    return;
                }
                addPaymentAccount();
                break;
        }
    }

    private void addPaymentAccount() {
        JSONObject object = new JSONObject();
        try {
            object.put("paymentType", paymentType);
            object.put("paymentAccount", edtCardCode.getText().toString());
            object.put("paymentName", edtCodeName.getText().toString());
            object.put("userId", SPUtils.getStringByKey(AddPaymentAccountActivity.this, Constants.UserId));
            String paramJson = object.toString();
            Logger.i(Logger.LOGTAG + "添加支付方式", paramJson);

            ServerApi.getString(Urls.addMyPayment, "", AESUtil.encrypt(paramJson))//
                    .subscribeOn(Schedulers.io())//
                    .doOnSubscribe(new Consumer<Disposable>() {
                        @Override
                        public void accept(@NonNull Disposable disposable) throws Exception {
                            showLoading("保存支付方式中...");
                        }
                    })//
                    .observeOn(AndroidSchedulers.mainThread())  //
                    .subscribe(new Observer<String>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {
                            addDisposable(d);
                        }

                        @Override
                        public void onNext(@NonNull String s) {
                            SimpleResponse resp = new Gson().fromJson(s, SimpleResponse.class);
                            if (null != resp) {
                                if (resp.code == 1) {
                                    showToast("保存成功");
                                    finish();
                                } else {
                                    showToast(resp.msg);
                                }
                            } else {
                                showToast("保存失败");
                            }
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            e.printStackTrace();            //请求失败
                        }

                        @Override
                        public void onComplete() {
                            dismissLoading();
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
